﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.DS
{
    public static partial class status
    {
        public static dynamic submitDSCode(int eid, int pid, int lang, string code, string username, bool god)
        {
            var sql = new Sql.SqlDataContext();
            if (!sql.DSProblem.Any(row => row.eid == eid && row.pid == pid)) return new
                {
                    success = false,
                    msg = "题目不存在"
                };
            else if (!sql.Language.Any(row => row.code == lang)) return new
                 {
                     success = false,
                     msg = "语言不存在"
                 };
            var e = sql.DSExperiment.First(row => row.eid == eid);
            if (e.startTime != null)
                if (e.startTime.Value > DateTime.Now) return new
                   {
                       success = false,
                       msg = "实验未开始，请在实验开始时间之后提交。"
                   };
            if (e.endTime <= DateTime.Now) return new
               {
                   success = false,
                   msg = "提交超出截止时间，实验已结束。"
               };
            var u = sql.User.First(row => row.username == username);

            var s = new Sql.DSStatus
            {
                eid = eid,
                pid = pid,
                lang = lang,
                code = code,
                username = username,
                sid = u.sid,
                status = 0,
                submitTime = DateTime.Now,
                god = god
            };
            sql.DSStatus.InsertOnSubmit(s);
            sql.SubmitChanges();
            return new
            {
                success = true,
                s.rid
            };
        }
    }
}
